package ca.krakenpower.kvoxels;

import java.io.*;
import java.text.DateFormat;
import java.util.Date;

public abstract class Logger {
	
	public enum Type {
		
		STDOUT {
			@Override
			public PrintStream getPrintStream() {
				return System.out;
			}
		},
		FILE {
			@Override
			public PrintStream getPrintStream() throws IOException {
				File log = new File("log.txt");
				if (log.exists()) {
					log.delete();
				}
				log.createNewFile();
				return new PrintStream(log);
			}
		},
		;
		
		public abstract PrintStream getPrintStream() throws IOException;
		
	}
	
	private static PrintStream output = null;
	private static DateFormat dateFormat = null;
	
	public static boolean initLogger(Type type) {
		boolean success = false;
		dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.DEFAULT);
		
		try {
			output = type.getPrintStream();

			log(type + " logger initialized.");
			success = true;
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return success;
		
	}
	
	public static void shutdown() {
		output.close();
	}
	
	public static void log(String str) {
		output.println("[" + dateFormat.format(new Date()) + "]" +
				" " + str);
	}
	
	public static void main(String[] args) {
		Logger.initLogger(Type.STDOUT);
		Logger.log("test1");
		Logger.shutdown();
	}

}
